package com.zhujiaji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhujiaji.common.Result;
import com.zhujiaji.pojo.Book;
import com.zhujiaji.service.BookService;
import com.zhujiaji.vo.BookVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author 朱佳吉
 * @date 2024/3/16 17:18
 */
@RestController
@RequestMapping("/api/book")
@Api(tags = "书籍测试")
public class BookController {

    @Resource
    private BookService bookService;

    @GetMapping("/list")
    @ApiOperation(value = "根据条件查询书籍, 并分页")
    public Result<IPage<Book>> list(BookVo bookVo) {
        IPage<Book> iPage = new Page(bookVo.getPageNo(), bookVo.getPageSize());
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(bookVo.getName()), Book::getName, bookVo.getName());
        wrapper.like(!StringUtils.isEmpty(bookVo.getAuthor()), Book::getAuthor, bookVo.getAuthor());
        return Result.ok(bookService.getBaseMapper().selectPage(iPage, wrapper));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "通过id来删除书籍")
    public Result delete(@PathVariable Integer id) {
        boolean suc = bookService.removeBookById(id);
        if (suc) {
            return Result.builder().code(200).message("删除成功").build();
        }

        return Result.error("删除失败");
    }

    @PostMapping("save")
    @ApiOperation(value = "添加书籍")
    public Result save(@RequestBody Book book) {
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper();
        wrapper.eq(!ObjectUtils.isEmpty(book) && !StringUtils.isEmpty(book.getName()),
                Book::getName, book.getName());
        if (bookService.count(wrapper) > 0) {
            return Result.error("已存在该书籍");
        }

        if (ObjectUtils.isEmpty(book.getImgPath())) {
            book.setImgPath("https://netshopimg.oss-cn-beijing.aliyuncs.com/images/0/2020/07/31/description/O1CN01zKCFYD1KQrdM42tSN_%21%212468631159.jpg");
        }

        boolean suc = bookService.save(book);
        if (suc) {
            return Result.builder().code(200).message("添加成功").build();
        }

        return Result.error("添加失败");
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改书籍")
    public Result update(@RequestBody Book book) {

        if (ObjectUtils.isEmpty(book.getImgPath())) {
            book.setImgPath("https://netshopimg.oss-cn-beijing.aliyuncs.com/images/0/2020/07/31/description/O1CN01zKCFYD1KQrdM42tSN_%21%212468631159.jpg");
        }

        boolean suc = bookService.updateById(book);
        if (suc) {
            return Result.builder().code(200).message("修改成功").build();
        }
        return Result.error("修改失败");
    }
}
